<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "product".
 *
 * @property integer $id
 * @property integer $category_id
 * @property string $name
 * @property string $sku
 * @property integer $stock
 * @property string $weight
 * @property string $market_price
 * @property string $price
 * @property string $brief
 * @property string $content
 * @property string $thumb
 * @property string $image
 * @property string $origin
 * @property string $keywords
 * @property string $description
 * @property integer $type
 * @property integer $brand_id
 * @property string $star
 * @property integer $sales
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $created_by
 * @property integer $updated_by
 *
 * @property Comment[] $comments
 * @property Consultation[] $consultations
 * @property Favorite[] $favorites
 * @property OrderProduct[] $orderProducts
 * @property Category $category
 * @property ProductImage[] $productImages
 */
class Product extends \yii\db\ActiveRecord
{
    const IS_NEW = 1;
    const IS_HOT = 2;
    const IS_GIRT = 4;
    const IS_PROMOTION = 8;
    const IS_GROUP = 16;

    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;
    const STATUS_DELETED = -1;

    public static function getStatus($status = null)
    {
        $data = [
            self::STATUS_INACTIVE => '禁用',
            self::STATUS_ACTIVE => '启用',
            self::STATUS_DELETED => '已删除',
        ];
        if ($status !== null && isset($data[$status])) {
            return $data[$status];
        } else {
            return $data;
        }
    }

    /**
     * @param null $type
     * @return array|string
     */
    public static function getType($type = null)
    {
        $data = [
            self::IS_NEW => '新品',
            self::IS_HOT => '热销',
            self::IS_GIRT => '赠品',
            self::IS_PROMOTION => '促销',
            self::IS_GROUP => '团购',
        ];
        if ($type === null) {
            return $data;
        } else {
            $str = '';
            foreach ($data as $k => $v) {
                if ($type & $k) {
                    $str .= ' ' . $data[$k];
                }
            }
            return $str;
        }
    }


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'product';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['category_id', 'name', 'sku', 'created_at', 'updated_at', 'created_by', 'updated_by'], 'required'],
            [['category_id', 'stock', 'type', 'brand_id', 'sales', 'status', 'created_at', 'updated_at', 'created_by', 'updated_by'], 'integer'],
            [['weight', 'market_price', 'price', 'star'], 'number'],
            [['content', 'description'], 'string'],
            [['name'], 'string', 'max' => 128],
            [['sku'], 'string', 'max' => 64],
            [['brief', 'thumb', 'image', 'origin', 'keywords'], 'string', 'max' => 255],
            [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'category_id' => 'Category ID',
            'name' => 'Name',
            'sku' => 'Sku',
            'stock' => 'Stock',
            'weight' => 'Weight',
            'market_price' => 'Market Price',
            'price' => 'Price',
            'brief' => 'Brief',
            'content' => 'Content',
            'thumb' => 'Thumb',
            'image' => 'Image',
            'origin' => 'Origin',
            'keywords' => 'Keywords',
            'description' => 'Description',
            'type' => 'Type',
            'brand_id' => 'Brand ID',
            'star' => 'Star',
            'sales' => 'Sales',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'created_by' => 'Created By',
            'updated_by' => 'Updated By',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getComments()
    {
        return $this->hasMany(Comment::className(), ['product_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getConsultations()
    {
        return $this->hasMany(Consultation::className(), ['product_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFavorites()
    {
        return $this->hasMany(Favorite::className(), ['product_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getOrderProducts()
    {
        return $this->hasMany(OrderProduct::className(), ['product_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(Category::className(), ['id' => 'category_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProductImages()
    {
        return $this->hasMany(ProductImage::className(), ['product_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getBrand()
    {
        return $this->hasOne(Brand::className(), ['id' => 'brand_id']);
    }
}
